//
//  MainSecondTableViewCell.m
//  Beauty
//
//  Created by Snow on 16/9/3.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "MainSecondTableViewCell.h"
#import "MainSecondCollectionViewCell.h"
#import "GXMacro.h"
#import "GXNetWorkingParser.h"
#import "ModelOfMainSecond.h"
@interface MainSecondTableViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) NSMutableArray *arrayOfMainSecond;
@end
@implementation MainSecondTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self handleData];
        _layout = [[UICollectionViewFlowLayout alloc] init];
        _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
        [self.contentView addSubview:_collectionView];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.contentView.mas_top);
            make.left.mas_equalTo(self.contentView.mas_left);
            make.right.mas_equalTo(self.contentView.mas_right);
            make.bottom.mas_equalTo(self.mas_bottom);
        }];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerClass:[MainSecondCollectionViewCell class] forCellWithReuseIdentifier:@"MainSecondCollectionViewCell"];
    }
    return self;
}
- (void)handleData
{
    _arrayOfMainSecond = [NSMutableArray array];
[GXNetWorkingParser GETWithURL:@"http://www.wantease.com/api/index.php?act=micro&op=getPersonalLike_wap_1_5" par:nil success:^(id responseObject) {
    NSDictionary *dic = [responseObject objectForKey:@"datas"];
    NSArray *arr = [dic objectForKey:@"personal_like_list"];
    for (NSDictionary *diction in arr) {
        ModelOfMainSecond *model = [[ModelOfMainSecond alloc] init];
        [model setValuesForKeysWithDictionary:diction];
        [_arrayOfMainSecond addObject:model];
    }
    [_collectionView reloadData];
} filed:^(NSError *error) {
    
}];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _arrayOfMainSecond.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MainSecondCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MainSecondCollectionViewCell" forIndexPath:indexPath];
    ModelOfMainSecond *model = [_arrayOfMainSecond objectAtIndex:indexPath.row];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:model.member_avatar]];
    cell.labelOfName.text = model.member_truename;
    cell.labelOfFans.text = model.count_friend;
    return cell;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    _layout.itemSize = CGSizeMake((self.contentView.bounds.size.width - 20) / 2, self.contentView.bounds.size.height - 20);
}
@end
